using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VRCV
    {
        public DataRow Convert2RCV(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryRCV(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KBRMANDT"] = " ".PadRight(3 ); 
         dwReturn["KBRWERKS"] = " ".PadRight(4 ); 
         dwReturn["KBRTGINR"] = " ".PadRight(4 ); 
         dwReturn["KBRTGMON"] = " ".PadRight(2 ); 
         dwReturn["KBRTGDAY"] = " ".PadRight(2 ); 
         dwReturn["KBRTGLNR"] = " ".PadRight(0 ); 
         dwReturn["KBRQUITT"] = " ".PadRight(1 ); 
         dwReturn["KBRPRTFL"] = " ".PadRight(1 ); 
         dwReturn["KBRTGTIM"] = " ".PadRight(4 ); 
         dwReturn["KBRSNDRP"] = " ".PadRight(2 ); 
         dwReturn["KBRTNONR"] = " ".PadRight(1 ); 
         dwReturn["KBRSWERK"] = " ".PadRight(4 ); 
         dwReturn["KBRSNONR"] = " ".PadRight(1 ); 
         dwReturn["KBRTGFREE"] = " ".PadRight(8 ); 
         dwReturn["KBRTNNAM"] = " ".PadRight(6 ); 
         dwReturn["KBRSNNAM"] = " ".PadRight(6 ); 
         dwReturn["KBRTAPPL"] = " ".PadRight(5 ); 
         dwReturn["KBRSAPPL"] = " ".PadRight(5 ); 
         dwReturn["KBRDAT01"] = " ".PadRight(1000 ); 
         dwReturn["KBRDAT02"] = " ".PadRight(1000 ); 
         dwReturn["KBRSTATU"] = " ".PadRight(2 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapRCV(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveRCV(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryRCV(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapRCV(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertRCV(dwLocal, ITXDBVERSION);
            }
            UpdateRCV(dwLocal);
        }
        public void InsertRCV(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO RCV(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryRCV(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM RCV WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM RCV  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "RCV";
            Return = Result;
            return Return;
        }
        public void DeleteRCV(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM RCV   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateRCV(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE RCV SET "
                    +"KBRMANDT= :KBRMANDT,"
                    +"KBRWERKS= :KBRWERKS,"
                    +"KBRTGINR= :KBRTGINR,"
                    +"KBRTGMON= :KBRTGMON,"
                    +"KBRTGDAY= :KBRTGDAY,"
                    +"KBRTGLNR= :KBRTGLNR,"
                    +"KBRQUITT= :KBRQUITT,"
                    +"KBRPRTFL= :KBRPRTFL,"
                    +"KBRTGTIM= :KBRTGTIM,"
                    +"KBRSNDRP= :KBRSNDRP,"
                    +"KBRTNONR= :KBRTNONR,"
                    +"KBRSWERK= :KBRSWERK,"
                    +"KBRSNONR= :KBRSNONR,"
                    +"KBRTGFREE= :KBRTGFREE,"
                    +"KBRTNNAM= :KBRTNNAM,"
                    +"KBRSNNAM= :KBRSNNAM,"
                    +"KBRTAPPL= :KBRTAPPL,"
                    +"KBRSAPPL= :KBRSAPPL,"
                    +"KBRDAT01= :KBRDAT01,"
                    +"KBRDAT02= :KBRDAT02,"
                    +"KBRSTATU= :KBRSTATU,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KBRMANDT", OracleType.NVarChar).Value = dwData["KBRMANDT"].ToString(); 
          or1.Parameters.Add("KBRWERKS", OracleType.NVarChar).Value = dwData["KBRWERKS"].ToString(); 
          or1.Parameters.Add("KBRTGINR", OracleType.NVarChar).Value = dwData["KBRTGINR"].ToString(); 
          or1.Parameters.Add("KBRTGMON", OracleType.NVarChar).Value = dwData["KBRTGMON"].ToString(); 
          or1.Parameters.Add("KBRTGDAY", OracleType.NVarChar).Value = dwData["KBRTGDAY"].ToString(); 
          or1.Parameters.Add("KBRTGLNR", OracleType.NVarChar).Value = dwData["KBRTGLNR"].ToString(); 
          or1.Parameters.Add("KBRQUITT", OracleType.NVarChar).Value = dwData["KBRQUITT"].ToString(); 
          or1.Parameters.Add("KBRPRTFL", OracleType.NVarChar).Value = dwData["KBRPRTFL"].ToString(); 
          or1.Parameters.Add("KBRTGTIM", OracleType.NVarChar).Value = dwData["KBRTGTIM"].ToString(); 
          or1.Parameters.Add("KBRSNDRP", OracleType.NVarChar).Value = dwData["KBRSNDRP"].ToString(); 
          or1.Parameters.Add("KBRTNONR", OracleType.NVarChar).Value = dwData["KBRTNONR"].ToString(); 
          or1.Parameters.Add("KBRSWERK", OracleType.NVarChar).Value = dwData["KBRSWERK"].ToString(); 
          or1.Parameters.Add("KBRSNONR", OracleType.NVarChar).Value = dwData["KBRSNONR"].ToString(); 
          or1.Parameters.Add("KBRTGFREE", OracleType.NVarChar).Value = dwData["KBRTGFREE"].ToString(); 
          or1.Parameters.Add("KBRTNNAM", OracleType.NVarChar).Value = dwData["KBRTNNAM"].ToString(); 
          or1.Parameters.Add("KBRSNNAM", OracleType.NVarChar).Value = dwData["KBRSNNAM"].ToString(); 
          or1.Parameters.Add("KBRTAPPL", OracleType.NVarChar).Value = dwData["KBRTAPPL"].ToString(); 
          or1.Parameters.Add("KBRSAPPL", OracleType.NVarChar).Value = dwData["KBRSAPPL"].ToString(); 
          or1.Parameters.Add("KBRDAT01", OracleType.NVarChar).Value = dwData["KBRDAT01"].ToString(); 
          or1.Parameters.Add("KBRDAT02", OracleType.NVarChar).Value = dwData["KBRDAT02"].ToString(); 
          or1.Parameters.Add("KBRSTATU", OracleType.NVarChar).Value = dwData["KBRSTATU"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
